package au.com.handsunited.web.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.model.User;
import au.com.handsunited.service.SecurityService;
import au.com.handsunited.web.command.user.UserLoginCommand;
import au.com.handsunited.web.session.SessionSupport;

public class LoginController extends SimpleFormController {

//public class LoginController extends AbstractCommandController {

	private static Logger logger = LoggerFactory.getLogger(LoginController.class);

//	private String failureView;
//	private String successView;

	private SecurityService securityService;

	public LoginController() {
		setCommandClass(UserLoginCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		super.onSubmit(request, response, command, errors);

		UserLoginCommand userCredentials = (UserLoginCommand) command;
		ModelAndView mav = new ModelAndView();

		User user = securityService.login(userCredentials.getUsername(), userCredentials.getPassword());
		if (user != null) {
			SessionSupport.setLoggedInUser(request, user);
			mav.setViewName(getSuccessView());
			mav.addObject("entity", user);
		} else {
			mav.setViewName(getFormView());
			mav.addObject("userCredentials", userCredentials);
			mav.addObject("message", "Invalid username or password.");
		}

		return mav;
	}

//	/**
//	 * @see org.springframework.web.servlet.mvc.AbstractCommandController#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
//	 */
//	@Override
//	protected ModelAndView handle(HttpServletRequest request,
//			HttpServletResponse response, Object command, BindException errors)
//			throws Exception {
//
//		UserLoginCommand userLoginCommand = (UserLoginCommand) command;
//		ModelAndView mav = new ModelAndView();
//		User user = securityService.login(userLoginCommand.getUsername(), userLoginCommand.getPassword());
//		if (user != null) {
//			SessionSupport.setLoggedInUser(request, user);
//			mav.setViewName(userLoginCommand.getSuccessView());
//			mav.addObject("entity", user);
//		} else {
//			mav.setViewName(failureView);
//			mav.addObject("userCredentials", userLoginCommand);
//			mav.addObject("message", "Invalid username or password.");
//		}
//
//		return null;
//	}

	/**
	 *
	 * @param securityService
	 */
	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}

//	/**
//	 * @param failureView the failureView to set
//	 */
//	public void setFailureView(String failureView) {
//		this.failureView = failureView;
//	}
//
//	/**
//	 * @param successView the successView to set
//	 */
//	public void setSuccessView(String successView) {
//		this.successView = successView;
//	}

}
